//
//  StatusViewModel.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/27.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
/// 原创微博的cell id
let StatusNomalCellID = "StatusNomalCellId"

let StatusRetweedCellID = "StatusRetweedCellID"

//微博视图模型
class StatusViewModel: CustomStringConvertible {
    //微博模型
    var status: Status
    
    
    /// 缓存的行高
    lazy var rowHeight: CGFloat = {
        
        // 1. cell
        var cell: StatusCell
        if self.status.retweeted_status != nil {
            cell = StatusRetweedCell(style: .Default, reuseIdentifier: StatusRetweedCellID)
        } else {
            cell = StatusNomalCell(style: .Default, reuseIdentifier: StatusNomalCellID)
        }
        // 2. 记录高度
        return cell.rowHeight(self)
    }()
    /// 微博cell id
    var cellID: String {
        return status.retweeted_status == nil ? StatusNomalCellID : StatusRetweedCellID
    }
    
    /// 微博信息
    //微博图像
    var userIconImageURL: NSURL {
        return NSURL(string: (status.user?.profile_image_url!)!)!
    }
    /// 用户认证
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var userMemberImage: UIImage? {
        switch (status.user?.verified_type)! {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
    /// 用户姓名
    var userName: String {
        return (status.user?.screen_name)!
    }
    ///会员图像   会员等级 0-6
    var userVipImage: UIImage? {
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\((status.user?.mbrank)!)")
        }
        return nil
    }
    /// 缩略图URL数组
    var thumbnailUrls: [NSURL]?

    /// 转发微博信息
    var retweedText: String? {
        guard let s = status.retweeted_status else {
            return nil
        }
        return "@" + (s.user?.screen_name ?? "")  + ":" + (s.text ?? "")
    }
    
    ///构造函数
    init(status: Status){
        self.status = status
//        print(status.retweeted_status?.pic_urls)
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            thumbnailUrls = [NSURL]()
            for dict in urls {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                thumbnailUrls?.append(url!)
            }
        }
    }
    
    var description: String {
        return status.description
    }
    
    var descriptionkkk: String {
        return status.description + "配图 \(thumbnailUrls)"
    }
}